package ui.tempdao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import util.HibernateUtil;
import dao.EgnPosition;
import dao.EgnProduct;

public class EgnPositionDao {
	private final static Logger logger = LoggerFactory.getLogger(EgnPositionDao.class);

	public static EgnViewPosition getEgnViewPosition(String posiId) {
		EgnViewPosition rst ;
		EgnPosition position;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnMarketVari instance");
		Query qr = s.createQuery("from EgnPosition a where a.id.contractId=:param" );
		qr.setParameter("param", posiId);
		try {
			position = (EgnPosition)qr.uniqueResult();
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
		Query qr1 = s.createQuery("from EgnProduct b where b.productId =:param1");
		qr1.setParameter("param1", position.getProductId());
		try {
			rst = new EgnViewPosition(position, (EgnProduct)qr1.uniqueResult());
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
	public static EgnViewPosition getEgnViewPosition(EgnPosition position) {
		EgnViewPosition rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnMarketVari instance");
		Query qr1 = s.createQuery("from EgnProduct b where b.productId =:param1");
		qr1.setParameter("param1", position.getProductId());
		try {
			rst = new EgnViewPosition(position, (EgnProduct)qr1.uniqueResult());
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
}
